/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisualizerPanel.java
 *
 * Created on 14.05.2010, 15:19:44
 */
package trinomialtree.treevisualizer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;
import trinomialtree.gui.TrinomialTreeView.mode;
import trinomialtree.math.Node;
import trinomialtree.math.TrinomialTree;

/**
 *
 * @author pl
 */
public class VisualizerPanel extends javax.swing.JPanel {

    private TrinomialTree tree;

    /** Creates new form VisualizerPanel */
    public VisualizerPanel(TrinomialTree tree) {
        initComponents();

        //Setze je nach gewähltem Modus (Jahr, Monat, Tag) eine unterschiedliche Größe des Panels
        Dimension d = null;
        if (tree.selectedMode.equals(mode.YEAR)) {
            d = new Dimension(1300, 680);
        } else if (tree.selectedMode.equals(mode.MONTH)) {
            d = new Dimension(2600, 1200);
        } else {
            d = new Dimension(5200, 2400);
        }
        this.setSize(d);
        this.setPreferredSize(d);


        this.tree = tree;

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setBackground(Color.white);

        if (tree == null) {
            return;
        }

        int depth = tree.getDepth();

        int height = this.getHeight();
        int width = this.getWidth();


        int x_stepsize = width / (depth * 2);
        int y_stepsize = height / (depth * 2);

        int zeroCoordinate = height / 2;


        //Zeichenroutine links nach rechts
        for (int i = 0; i <= depth; i++) {

            for (int j = -i; j <= i; j++) {

                Node n = tree.getNodeFromList(i, j);

                //Ein Knoten könnte bereits vorher gelöscht worden sein, da er keine Vorgänger
                //hat
                if (n != null) {

                    int x = 10 + (i * x_stepsize);
                    int y = zeroCoordinate + (j * y_stepsize);
                    g.fillOval(x, y, 5, 5);

                    //Male eine Linie zu den Nachfolgerknoten dieses Knotens

                    if (n.up != null) {
                        int x1 = 10 + (n.up.i * x_stepsize);
                        int y1 = zeroCoordinate + (n.up.j * y_stepsize);
                        g.drawLine(x, y, x1, y1);
                    }

                    if (n.down != null) {
                        int x1 = 10 + (n.down.i * x_stepsize);
                        int y1 = zeroCoordinate + (n.down.j * y_stepsize);
                        g.drawLine(x, y, x1, y1);
                    }

                    if (n.middle != null) {
                        int x1 = 10 + (n.middle.i * x_stepsize);
                        int y1 = zeroCoordinate + (n.middle.j * y_stepsize);
                        g.drawLine(x, y, x1, y1);
                    }

                }



            }
        }





//        //Zeichenroutine - rechts nach links
//        for (int i = depth; i >= 0; i--) {
//            for (int j = -i; j <= i; j++) {
//
//                Node n = tree.getNodeFromList(i, j);
//                int x = 10 + (i * x_stepsize);
//                int y = zeroCoordinate + (j * y_stepsize);
//                g.fillOval(x, y, 5, 5);
//
//                //Male eine Linie zu den Vorgängerknoten dieses Knotens
//
//                // Hole pro Knoten die Queue mit den Vorgängerknoten
//                List<Node> parentNodes = n.getParentNodes();
//
//                for (Node parentNode: parentNodes) {
//                        int x1 = 10 + (parentNode.i * x_stepsize);
//                        int y1 = zeroCoordinate + (parentNode.j * y_stepsize);
//
//                        g.drawLine(x, y, x1, y1);
//                }
//            }
//        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(1300, 680));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1310, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 711, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
